package com.hue;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;

@SuppressWarnings("serial")
public class SaveLinks extends HttpServlet {
	public static final String SAVE_LINKS = "SAVE_LINKS";

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    	final String jsArray = req.getParameter("jsArray");
    	System.out.println(jsArray);
    	if(jsArray != null && !jsArray.isEmpty()) {
    		try {
	    		final JSONArray jsonArray = new JSONArray(jsArray);
	    		ArrayList links = (ArrayList) getServletContext().getAttribute(SAVE_LINKS);
	    		if(links == null) {
	    			links = new ArrayList();
	    			getServletContext().setAttribute(SAVE_LINKS, links);
	    		}
	    		for(int i=0; i<jsonArray.length(); i++) {
	    			final String s = jsonArray.getString(i);
	    			if(!links.contains(s)) {
	    				links.add(s);
	    			}
	    		}
    		}
    		catch(JSONException ex) {
    			System.out.println(ex.toString());
    		}
    	}
    }
}
